﻿import com.dmotiko.zonaoutlet.*
import com.dmotiko.general.*
import mx.utils.Delegate;

class ZOBuscador
extends ZOSiteSection {
	
	private var mcSelector:SelectorClip;
	private var mcOutletList:ZOOutletList;
	private var mcSpecial1:ZOOutletSpecial;
	private var mcSpecial2:ZOOutletSpecial;
	private var mcSpecial3:ZOOutletSpecial;
	private var mcSpecial4:ZOOutletSpecial;
	private var mcOutletTemplate:ZOOutletTemplate;
		
	function ZOBuscador() {
		super( ZOSite.getApp() );
		sName = ZOSite.BUSCADOR;
		sectionChanged();
	}
		
	public function childLoaded( e ):Void {
		switch( e.target ) {
			case mcSelector:
			var aData:Array = ZOSite.getApp().getTypesData(true);
			mcSelector.setData( aData );
			mcSelector.setLabel("seleccione rubro");
			mcSelector.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, selector_changed ) );
			mcSelector.setSelectedItem( aData[0] );
			if ( ZOSite.getApp().getActiveOutlet() ) gotoAndStop(3);
			break;
			
			case mcSpecial1:
			if ( ZOSite.getApp().getOutletsData("special")[0] ) mcSpecial1.setData( ZOSite.getApp().getOutletsData("special")[0] );
			else mcSpecial1._visible = false;
			mcSpecial1.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, special_selected ) );
			break;
			
			case mcSpecial2:
			if ( ZOSite.getApp().getOutletsData("special")[1] ) mcSpecial2.setData( ZOSite.getApp().getOutletsData("special")[1] );
			else mcSpecial2._visible = false;
			mcSpecial2.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, special_selected ) );
			break;
			
			case mcSpecial3:
			if ( ZOSite.getApp().getOutletsData("special")[2] ) mcSpecial3.setData( ZOSite.getApp().getOutletsData("special")[2] );
			else mcSpecial3._visible = false;
			mcSpecial3.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, special_selected ) );
			break;
			
			case mcSpecial4:
			if ( ZOSite.getApp().getOutletsData("special")[3] ) mcSpecial4.setData( ZOSite.getApp().getOutletsData("special")[3] );
			else mcSpecial4._visible = false;
			mcSpecial4.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, special_selected ) );
			break;
			
			case mcOutletTemplate:
			mcOutletTemplate.setData( ZOSite.getApp().getActiveOutlet() );
			ZOSite.getApp().setActiveOutlet();
			break;
			
		}
	}
	
	private function special_selected( evnt ):Void {
		ZOSite.getApp().setActiveOutlet( evnt.data );
		gotoAndStop(3);
	}
	
	private function selector_changed( evnt ):Void {
		mcOutletList.setData( ZOSite.getApp().getOutletsData( evnt.data.value) );
		mcOutletList.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, list_changed ) );
	}
	
	private function list_changed( evnt ):Void {
		ZOSite.getApp().setActiveOutlet( evnt.selectedItem.value );
		if ( _currentframe == 2 ) {
			gotoAndStop(3);
		} else {
			gotoAndStop(4);
			gotoAndStop(3);
		}
	}		
}